package com.hz.monitoring.module.hardware.convert.sensor_sys_type;

import com.hz.monitoring.framework.common.pojo.PageResult;
import com.hz.monitoring.framework.common.util.json.JsonUtils;
import com.hz.monitoring.module.hardware.controller.admin.sensor_sys_type.vo.*;
import com.hz.monitoring.module.hardware.controller.admin.vo.CustomFields;
import com.hz.monitoring.module.hardware.controller.admin.vo.CustomSensorAttribute;
import com.hz.monitoring.module.hardware.dal.dataobject.sensor_sys_type.SensorSysTypeDO;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 传感器系统分类 Convert
 *
 * @author lin
 */

public class SensorSysTypeConvert {


  
    public static SensorSysTypeDO convert(SensorSysTypeCreateReqVO bean) {
        if (bean == null ) {
            return null;
        }

        String attributeMetadata = null;
        if (Objects.nonNull(bean.getAttributeMetadata())) {
            attributeMetadata = JsonUtils.toJsonString(bean.getAttributeMetadata());
        }
        String valueMetadata = null;
        if (Objects.nonNull(bean.getValueMetadata())) {
            valueMetadata = JsonUtils.toJsonString(bean.getValueMetadata());
        }

        SensorSysTypeDO sensorSysTypeDO = new SensorSysTypeDO();
        sensorSysTypeDO.setMcuModel( bean.getMcuModel() );
        sensorSysTypeDO.setName( bean.getName() );
        sensorSysTypeDO.setModel( bean.getModel() );
        sensorSysTypeDO.setVendor( bean.getVendor() );
        sensorSysTypeDO.setEdition( bean.getEdition() );
        sensorSysTypeDO.setSpecification( bean.getSpecification() );
        sensorSysTypeDO.setAttributeMetadata(attributeMetadata);
        sensorSysTypeDO.setValueMetadata(valueMetadata);

        return sensorSysTypeDO;
    }

  
    public static SensorSysTypeDO convert(SensorSysTypeUpdateReqVO bean) {
        if ( bean == null ) {
            return null;
        }
        String attributeMetadata = null;
        if (Objects.nonNull(bean.getAttributeMetadata())) {
            attributeMetadata = JsonUtils.toJsonString(bean.getAttributeMetadata());
        }
        String valueMetadata = null;
        if (Objects.nonNull(bean.getValueMetadata())) {
            valueMetadata = JsonUtils.toJsonString(bean.getValueMetadata());
        }
        SensorSysTypeDO sensorSysTypeDO = new SensorSysTypeDO();

        sensorSysTypeDO.setId( bean.getId() );
        sensorSysTypeDO.setMcuModel( bean.getMcuModel() );
        sensorSysTypeDO.setName( bean.getName() );
        sensorSysTypeDO.setModel( bean.getModel() );
        sensorSysTypeDO.setVendor( bean.getVendor() );
        sensorSysTypeDO.setEdition( bean.getEdition() );
        sensorSysTypeDO.setSpecification( bean.getSpecification() );
        sensorSysTypeDO.setAttributeMetadata(attributeMetadata);
        sensorSysTypeDO.setValueMetadata(valueMetadata);
        return sensorSysTypeDO;
    }

  
    public static SensorSysTypeRespVO convert(SensorSysTypeDO bean) {
        if ( bean == null ) {
            return null;
        }
        List<CustomSensorAttribute> attributeMetadata = new ArrayList<>();
        if (StringUtils.hasLength(bean.getAttributeMetadata())) {
            attributeMetadata = JsonUtils.parseArray(bean.getAttributeMetadata(), CustomSensorAttribute.class).stream().sorted(Comparator.comparing(CustomSensorAttribute::getSorts)).collect(Collectors.toList());
        }
        List<CustomFields> valueMetadata = new ArrayList<>();
        if (StringUtils.hasLength(bean.getValueMetadata())) {
            valueMetadata = JsonUtils.parseArray(bean.getValueMetadata(), CustomFields.class).stream().sorted(Comparator.comparing(CustomFields::getSorts)).collect(Collectors.toList());
        }

        SensorSysTypeRespVO sensorSysTypeRespVO = new SensorSysTypeRespVO();
        sensorSysTypeRespVO.setMcuModel( bean.getMcuModel() );
        sensorSysTypeRespVO.setName( bean.getName() );
        sensorSysTypeRespVO.setModel( bean.getModel() );
        sensorSysTypeRespVO.setVendor( bean.getVendor() );
        sensorSysTypeRespVO.setEdition( bean.getEdition() );
        sensorSysTypeRespVO.setAttributeMetadata(attributeMetadata);
        sensorSysTypeRespVO.setValueMetadata(valueMetadata);
        sensorSysTypeRespVO.setSpecification( bean.getSpecification() );
        sensorSysTypeRespVO.setId( bean.getId() );
        sensorSysTypeRespVO.setCreateTime( bean.getCreateTime() );

        return sensorSysTypeRespVO;
    }

  
    public static List<SensorSysTypeRespVO> convertList(List<SensorSysTypeDO> list) {
        if ( list == null ) {
            return null;
        }

        List<SensorSysTypeRespVO> list1 = new ArrayList<SensorSysTypeRespVO>( list.size() );
        for ( SensorSysTypeDO sensorSysTypeDO : list ) {
            list1.add( convert( sensorSysTypeDO ) );
        }

        return list1;
    }

  
    public static PageResult<SensorSysTypeRespVO> convertPage(PageResult<SensorSysTypeDO> page) {
        if ( page == null ) {
            return null;
        }

        PageResult<SensorSysTypeRespVO> pageResult = new PageResult<SensorSysTypeRespVO>();

        pageResult.setList( convertList( page.getList() ) );
        pageResult.setTotal( page.getTotal() );

        return pageResult;
    }

  
    public static List<SensorSysTypeExcelVO> convertList02(List<SensorSysTypeDO> list) {
        if ( list == null ) {
            return null;
        }

        List<SensorSysTypeExcelVO> list1 = new ArrayList<SensorSysTypeExcelVO>( list.size() );
        for ( SensorSysTypeDO sensorSysTypeDO : list ) {
            list1.add( sensorSysTypeDOToSensorSysTypeExcelVO( sensorSysTypeDO ) );
        }

        return list1;
    }

    public static SensorSysTypeExcelVO sensorSysTypeDOToSensorSysTypeExcelVO(SensorSysTypeDO sensorSysTypeDO) {
        if ( sensorSysTypeDO == null ) {
            return null;
        }

        SensorSysTypeExcelVO sensorSysTypeExcelVO = new SensorSysTypeExcelVO();
        sensorSysTypeExcelVO.setId( sensorSysTypeDO.getId() );
        sensorSysTypeExcelVO.setMcuModel( sensorSysTypeDO.getMcuModel() );
        sensorSysTypeExcelVO.setName( sensorSysTypeDO.getName() );
        sensorSysTypeExcelVO.setModel( sensorSysTypeDO.getModel() );
        sensorSysTypeExcelVO.setVendor( sensorSysTypeDO.getVendor() );
        sensorSysTypeExcelVO.setEdition( sensorSysTypeDO.getEdition() );
        sensorSysTypeExcelVO.setSpecification( sensorSysTypeDO.getSpecification() );
        sensorSysTypeExcelVO.setCreateTime( sensorSysTypeDO.getCreateTime() );
        return sensorSysTypeExcelVO;
    }

}
